package com.danbay.framework.utils;

/**
 * @author SJG
 *         2017/11/21.
 */
public class ByteUtils {

    /**
     *  long转字节数组
     * @param num long
     * @return byte[]
     */
    public static byte[] long2Bytes(long num) {
        byte[] byteNum = new byte[8];
        for (int ix = 0; ix < 8; ++ix) {
            int offset = 64 - (ix + 1) * 8;
            byteNum[ix] = (byte) ((num >> offset) & 0xff);
        }
        return byteNum;
    }

    /**
     * 字节数组转Long
     * @param byteNum 字节数组
     * @return long
     */
    public static long bytes2Long(byte[] byteNum) {
        long num = 0;
        for (int ix = 0; ix < 8; ++ix) {
            num <<= 8;
            num |= (byteNum[ix] & 0xff);
        }
        return num;
    }


    public static void main(String[] args) {
        byte[] bytes=new byte[]{0x70,0x09,0x00, (byte) 0xd4,0x18,0x7f,0x00,0x00};
        System.out.println(bytes2Long(bytes));
        System.out.println(long2Bytes(8072984717982367744L).length);
    }
}
